#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: slp
# @Date:   2018-05-04 15:40:55
# @Last Modified by:   slp
# @Last Modified time: 2018-05-04 15:42:56

import urllib
import urllib2
import sys
import re
import json

def http_conn(url, data):
	request = urllib2.Request(url, data)
	request.add_header("Content-Type", "application/json")
	ret = urllib2.urlopen(request)
	if ret.getcode() < 200 or ret.getcode() >= 300:
		raise Exception("http request not success, code:%d"%(ret.getcode()))
	return ret.read()

def main():
	url = "http://202.197.154.243:5050/api/v1/executor"
	data = {}
	data["type"] = "SUBSCRIBE"
	# data["subscribe"] = {"framework_info":{"user":"foo","name":"example http", "roles":["test"], "capabilities":[{"type":"MULTI_ROLE"}]}}
	data["subscribe"] = {"framework_info":{"user":"foo","name":"example http", "roles":[], "capabilities":[]}}

	# data = "SUBSCRIBE"
	data = json.dumps(data)
	print data
	r = http_conn(url, data)
	# r = http_conn(url, GET=True)
	print r

if __name__ == '__main__':
	main()
